var $headerMenu = $("#headerMenu");

function initTitle() {
    var title = $("#navTitle").text() || document.title;
    $('li', $headerMenu).removeClass("active");
    $("a span", $headerMenu).each(function() {
        var text = $(this).text();
        if (text == title) {
            $item = $(this).parents('li').addClass('active');
            return false;
        }
    });
}
const $alert = $("#alert");

function doAlert(msg, timeout, type, callback) {
    $(".js-alert-content", $alert).text(msg);
    if (!type) {
        type = 'danger';
    }
    type && $(".js-alert-content").removeClass().addClass('modal-body js-alert-content alert alert-' + type);
    $alert.modal('show');
    if (timeout > 0) {
        setTimeout(function() {
            $alert.modal('hide');
            if (typeof callback == 'function') {
                callback();
            }
        }, timeout);
    } else {
        document.onkeydown = function(event) {
            var e = event || window.event || arguments.callee.caller.arguments[0];
            if (e && e.keyCode == 13) {
                $alert.modal('hide');
            }
        };
    }
    return 'success' == type;
}

// 多个模态框的滚动条混乱问题，一个模态框关闭时，会导致其他模态框滚动条消失
var modalShownCount = 0;
$('body').on('shown.bs.modal', 'div.modal', function(e) {
    modalShownCount++;
});
$('body').on('hidden.bs.modal', 'div.modal', function(e) {
    console.info(modalShownCount)
    modalShownCount--;
    if (modalShownCount > 0) {
        $("body").addClass("modal-open");
    }
});


function serializeArrayToJson(a) {
    var options = {};
    for (var i = 0; i < a.length; i++) {
        var value = a[i].value;
        if ($.trim(value) != '') {
            if (!options[a[i].name]) {
                options[a[i].name] = a[i].value;
            } else {
                if (typeof options[a[i].name] == 'object') {
                    options[a[i].name].push(a[i].value)
                } else {
                    options[a[i].name] = [options[a[i].name], a[i].value]
                }
            }
        }
    }
    return options;
}

var J30 = '';

function parseJ20() {
    if (typeof J20 == 'undefined') {
        return false;
    }
    var t = atob(J20);
    J30 = decodeURIComponent(t);
}

function showBtns() {
    $('*[data-permissions]').each(function() {
        var v = $(this).attr('data-permissions');
        if (J30.indexOf(v) >= 0) {
            $(this).show();
        }
    })
}

function aboutIE() {
    if (!Array.prototype.forEach) {
        Array.prototype.forEach = function forEach(callback, thisArg) {
            var T, k;
            if (this == null) {
                throw new TypeError("this is null or not defined");
            }
            var O = Object(this);
            var len = O.length >>> 0;
            if (typeof callback !== "function") {
                throw new TypeError(callback + " is not a function");
            }
            if (arguments.length > 1) {
                T = thisArg;
            }
            k = 0;
            while (k < len) {

                var kValue;
                if (k in O) {
                    kValue = O[k];
                    callback.call(T, kValue, k, O);
                }
                k++;
            }
        };
    }
}
aboutIE();
//多个模态框的滚动条混乱问题，一个模态框关闭时，会导致其他模态框滚动条消失
var modalShownCount = 0;
$('body').on('shown.bs.modal', 'div.modal', function(e) {
    modalShownCount++;
});
$('body').on('hidden.bs.modal', 'div.modal', function(e) {
    modalShownCount--;
    if (modalShownCount > 0) {
        $("body").addClass("modal-open");
    } else {
        $("body").removeClass("modal-open");
    }
});
$(document).ready(function() {
    initTitle();
    parseJ20();
    showBtns();
})